<?php

/**
 * ECSHOP 数据采集安装程序
 * ============================================================================
 * $Author: YunGaZeon $
 * $Blog: http://www.cnblogs.com/GaZeon/ $
 * $Id: lib_collection.php start:2017-08-14 end:2017-08-14 YunGaZeon $
*/
header("Content-type: text/html; charset=utf-8"); 
define('IN_ECS', true);
require(dirname(__FILE__) . '/../includes/init.php');

$IS_FINISH=TRUE;

streamout('<br>-------------------------开始安装-------------------------</br>');

//核心文件
copyto('collection.php','../admin/collection.php');
copyto('lib_format.php','../admin/includes/lib_format.php');
copyto('lib_collection.php','../admin/includes/lib_collection.php');

//模板
copyto('collection_content_list.htm','../admin/templates/collection_content_list.htm');
copyto('collection_info.htm','../admin/templates/collection_info.htm');
copyto('collection_list.htm','../admin/templates/collection_list.htm');
//语言文件
copyto('languages/collection.php','../languages/zh_cn/admin/collection.php');

//添加权限和菜单
$sets="
/**********商品信息采集管理 @YunGaZeon***********/
\$modules['20_collection']['01_node_manage']        = 'collection.php?act=node_manage';
\$modules['20_collection']['02_node_add']        = 'collection.php?act=node_add';
\$modules['20_collection']['03_node_content_manage']        = 'collection.php?act=content_list';
";
config('添加菜单项','../admin/includes/inc_menu.php',$sets);


$sets="
/**********商品信息采集管理 @YunGaZeon***********/
	\$purview['01_node_manage']        = ['node_manage','node_collect','node_test'];
	\$purview['02_node_add'] = 'node_manage';
	\$purview['03_node_content_manage'] = ['content_manage','content_collect','content_implode'];
";
config('添加权限映射配置','../admin/includes/inc_priv.php',$sets);



$sets='
/**********商品信息采集管理 @YunGaZeon***********/
$_LANG[\'20_collection\'] = \'采集管理\';
$_LANG[\'01_node_manage\'] = \'节点列表\';
$_LANG[\'02_node_add\'] = \'添加节点\';
$_LANG[\'03_node_content_manage\'] = \'内容管理\';
';

config('添加菜单语言配置','../languages/zh_cn/admin/common.php',$sets);


$sets="
/**********商品信息采集管理 @YunGaZeon***********/
\$_LANG['node_manage']        = '节点增删改查';
\$_LANG['node_collect']        = '采集网址';
\$_LANG['content_collect']        = '采集网址内容';
\$_LANG['content_manage']        = '采集内容增删改查管理';
\$_LANG['content_implode']        = '导出采集内容';
\$_LANG['node_test']        = '测试采集网址，网址内容';
";
config('添加权限语言配置','../languages/zh_cn/admin/priv_action.php',$sets);


//插入权限
streamout('导入权限到admin_action表...');
$sql="select count(action_id) from {$ecs->table('admin_action')} where action_code='collection'";
if($db->getOne($sql)<=0)
{
	

	$sql="insert into {$ecs->table('admin_action')} (parent_id,action_code) VALUES (0,'collection')";
	if($db->query($sql))
	{
		$parent_id=$db->insert_id();
		$sql="insert into {$ecs->table('admin_action')} (parent_id,action_code) VALUES 
			({$parent_id},'node_manage'),
			({$parent_id},'node_collect'),
			({$parent_id},'content_manage'),
			({$parent_id},'content_collect'),
			({$parent_id},'node_test')";
		if($db->query($sql))
			streamout('成功<br/>');
		else
		{
			$IS_FINISH=FALSE;
			streamout('失败<br/>'); 
		}
	}
	else
		streamout('导入父权限失败：'.$db->error().'<br/>');

}else
	streamout('已存在，跳过<br/>');

//插入表
streamout('新建节点表collection_node表...');
$db->query("drop table if exists {$prefix}collection_node");
$sql="create table `{$prefix}collection_node`
(
id smallint unsigned auto_increment primary key,
/*采集配置*/
name varchar(50) not null default '',
lastdate int unsigned not null default 0,
siteid smallint unsigned not null default 0,
catid int unsigned not null default 0 comment '采集商品所属类型',
sourcecharset varchar(8) not null default '',
sourcetype tinyint unsigned not null,
page_sign_rule text comment '该网址url标识获取规则',
urlpage text comment '序列网址',
urlpage_single text comment '单行网址',
urlpage_multi text comment '多行网址',
urlpage_json text comment 'JSON内容网址',
pagesize_start tinyint not null default 0,
pagesize_end mediumint not null default 0,
par_num tinyint unsigned not null default 0,
url_contain char(100) not null default '',
url_except char(100) not null default '',
url_start char(100) not null default '',
url_title_contain varchar(50) not null default '',
url_title_except varchar(50) not null default '',

url_end char(100) not null default '',

text_title_sign varchar(50) not null default '',
text_url_sign varchar(50) not null default '',
text_webhead varchar(50) not null default '',

/*每次获取条数*/
per_collect_num mediumint unsigned not null default 0 comment '每次获取限制条数，0为不限制',
is_create_brand tinyint unsigned not null default 0 comment '是否创建品牌',

/*通用获取规则*/
rule_goods_name text,
rule_shop_price text,	
rule_brand text,
rule_goods_weight text,
rule_goods_desc text,
rule_goods_img text,

rule_goods_comment text,

/*通用包含规则*/
include_goods_name text,
include_shop_price text,	
include_brand text,
include_goods_weight text,
include_goods_desc text,
include_goods_img text,

include_goods_comment text,

/*通用排除规则*/
except_goods_name text,
except_shop_price text,	
except_brand text,
except_goods_weight text,
except_goods_desc text,
except_goods_img text,

except_goods_comment text,

/*通用移除规则*/
replace_goods_name text,replaceto_goods_name text,
replace_shop_price text,replaceto_shop_price text,	
replace_brand text,replaceto_brand text,
replace_goods_weight text,replaceto_goods_weight text,
replace_goods_desc text,replaceto_goods_desc text,
replace_goods_img text,replaceto_goods_img text,

replace_goods_comment text,replaceto_goods_comment text,

/*商品相册规则*/
is_gallery_async tinyint unsigned not null default 0 comment '是否异步加载',
gallery_url_get_type tinyint unsigned not null default 0 comment 'URL获取方式：0拼接url 1正则获取',
json_gallery_async_url text comment '获取相册JSON的url',
rule_gallery_async_url text,
include_gallery_async_url text,
except_gallery_async_url text,
replace_gallery_async_url text,
replaceto_gallery_async_url text,
gallery_data_type tinyint unsigned not null default 0 comment '采集到的相册数据类型：0html，1json',
gallery_start text comment '相册内容开始字符位置',
gallery_end text comment '相册内容结束字符位置',
rule_goods_gallery text,
include_goods_gallery text,
except_goods_gallery text,
replace_goods_gallery text,replaceto_goods_gallery text,
gallery_data_jsonp tinyint unsigned not null default 0 comment '是否去除jsonp：0否，1是',
gallery_data_key varchar(20) not null default '' comment '格式化JSON数组后获取详情正文的键名',
gallery_num_limit tinyint unsigned not null default 0 comment '相册数量限制 0为无限',
is_gallery_download tinyint unsigned not null default 0 comment '是否下载图片',
gallery_src_type tinyint unsigned not null default 0 comment '0单层多条，1单层单条，2多层多条，若为1则配合gallery_src_type_separator，若为2则配合gallery_src_type_key',
gallery_src_type_separator char(4) not null default '' comment '单层单条分隔符',
gallery_src_type_key varchar(20) not null default '' comment '多层多条的单个图片所在的键名或下标',


/*商品详情高级获取规则*/
is_desc_lazyload tinyint unsigned not null default 0 comment '延迟加载商品详情',

desc_url_get_type tinyint unsigned not null default 0 comment '详情url获取方式：0拼接url 1正则获取',
desc_data_type tinyint unsigned not null default 0 comment '采集到的详情数据类型：0html，1json',

desc_data_jsonp tinyint unsigned not null default 0 comment '是否去除jsonp：0否，1是',

desc_data_key varchar(20) not null default '' comment '格式化JSON数组后获取详情正文的键名',

json_desc_lazyload_url text comment '获取详情JSON的url',

rule_desc_lazyload_url text comment '延迟加载商品详情的url',
include_desc_lazyload_url text comment '该url必须包含',
except_desc_lazyload_url text comment '该url必须不包含',
replace_desc_lazyload_url varchar(100) not null default '' comment '替换内容中字符', 
replaceto_desc_lazyload_url varchar(100) not null default '' comment '替换为', 

/*商品评价高级获取规则*/
comment_pagenum tinyint unsigned not null default 1 comment '允许获取最多评价页数',
comment_issuenum tinyint unsigned not null default 1 comment '允许获取最多评价条目数',

comment_url_get_type tinyint unsigned not null default 0 comment '评价url获取方式：0html，1json',

json_comment_multiload_url text comment '获取加载评价的url',

comment_data_type tinyint unsigned not null default 0 comment '采集到的评价数据类型：0html，1json',

comment_start text comment '评价总开始',
comment_end text comment '评价总结束',

comment_one_start text comment '单评价开始',
comment_one_end text comment '单评价结束',

is_comment_multiload tinyint unsigned not null default 0 comment '是否延迟加载商品评价',
rule_comment_multiload_url text comment '延迟加载商品评价的url',
include_comment_multiload_url text comment '该url必须包含',
except_comment_multiload_url text comment '该url必须不包含',
replace_comment_multiload_url varchar(100) not null default '' comment '替换内容中字符',
replaceto_comment_multiload_url varchar(100) not null default '' comment '字符替换为', 

comment_data_jsonp tinyint unsigned not null default 0 comment '是否去除jsonp：0否，1是',

comment_data_key varchar(20) not null default '' comment '格式化JSON数组后获取详情正文的键名',

rule_comment_user text,
include_comment_user text,
except_comment_user text,
replace_comment_user text,replaceto_comment_user text,
is_add_comment_user tinyint unsigned not null default 0 comment '是否添加评价临时用户：0否，1是',

rule_comment_content text,
include_comment_content text,
except_comment_content text,
replace_comment_content text,replaceto_comment_content text,

rule_comment_rank text,
include_comment_rank text,
except_comment_rank text,
replace_comment_rank text,replaceto_comment_rank text,

rule_comment_time text,
include_comment_time text,
except_comment_time text,
replace_comment_time text,replaceto_comment_time text,
is_timestamp_format tinyint unsigned not null default 1 comment '是否格式化为时间戳',

rule_comment_extimg text,
include_comment_extimg text,
except_comment_extimg text,
replace_comment_extimg text,replaceto_comment_extimg text,

/*是否下载描述和评价中的图片规则*/
desc_img_download tinyint unsigned not null default 0 comment '是否下载描述内容的图片',
comment_img_download tinyint unsigned not null default 0 comment '是否下载评价内的图片',
comment_extimg_download tinyint unsigned not null default 0 comment '是否下载评价附属图片',

/*图片url配置规则*/
img_src_names varchar(100) not null default 'src' comment '采集图片src名，以逗号分隔',
goods_img_url_completion varchar(50) not null default '' comment '商品图片src补全',
gallery_img_url_completion varchar(50) not null default '' comment '商品相册src补全',
desc_img_url_completion varchar(50) not null default '' comment '商品详情图片url补全',
comment_img_url_completion varchar(50) not null default '' comment '商品评价图片src补全'


)engine=myisam,charset=utf8;";

if($db->query($sql))
	streamout('成功<br/>');
else
{
	$IS_FINISH=FALSE;
	streamout('失败<br/>'); 
}

streamout('新建自定义规则表collection_diyrule...');
$db->query("drop table if exists {$prefix}collection_diyrule");
$sql="create table `{$prefix}collection_diyrule`
(
id smallint unsigned auto_increment primary key,
nodeid smallint unsigned not null default 0 comment '所属节点id',
attrid smallint unsigned not null default 0 comment '所属属性id',
attrname varchar(50) not null default '',
rule_content varchar(100) not null default '' comment '匹配规则',
rule_include varchar(100) not null default '' comment '必须包含字符规则',
rule_except varchar(100) not null default '' comment '必须不包含字符规则',
rule_replace varchar(100) not null default '' comment '替换字符规则',
rule_replaceto varchar(100) not null default '' comment '替换为...'
)";

if($db->query($sql))
	streamout('成功<br/>');
else
{
	$IS_FINISH=FALSE;
	streamout('失败<br/>'); 
}


streamout('新建网址采集表collection_site...');
$db->query("drop table if exists {$prefix}collection_site");
$sql="create table `{$prefix}collection_site`
(
id smallint unsigned auto_increment primary key,
nodeid smallint unsigned not null default 0 comment '所属节点id',
sign varchar(30) not null default '' comment '该网址url标识',
title varchar(50) not null default '' comment '获取的网址标题',
url varchar(100) not null default '' comment '网址url',
is_read enum('0','1') not null default '0' comment '是否已经被采集网址内容'
)";
if($db->query($sql))
	streamout('成功<br/>');
else
{
	$IS_FINISH=FALSE;
	streamout('失败<br/>'); 
}


streamout('新建自定义字段表collection_field...');
$db->query("drop table if exists {$prefix}collection_field");
$sql="create table `{$prefix}collection_field`
(
id smallint unsigned auto_increment primary key,
nodeid smallint unsigned not null default 0,
field_name varchar(50) not null default '' comment '自定义字段名',
field_rule_match varchar(100) not null default '' comment '匹配规则',
field_rule_include varchar(100) not null default '' comment '必须包含字符规则',
field_rule_except varchar(100) not null default '' comment '必须不包含字符规则',
field_rule_replace varchar(100) not null default '' comment '替换字符规则',
field_rule_replaceto varchar(100) not null default '' comment '替换为...'
)";
if($db->query($sql))
	streamout('成功<br/>');
else
{
	$IS_FINISH=FALSE;
	streamout('失败<br/>'); 
}	

streamout('新建公共替换表collection_replace...');
$db->query("drop table if exists {$prefix}collection_replace");
$sql="create table `{$prefix}collection_replace`
(
id smallint unsigned auto_increment primary key,
nodeid smallint unsigned not null default 0,
replace_from text comment '需要替换的字符，多个字符以,号分隔',
replace_to varchar(50) not null default '' comment '要替换为'
)";
if($db->query($sql))
	streamout('成功<br/>');
else
{
	$IS_FINISH=FALSE;
	streamout('失败<br/>'); 
}

streamout('新建采集内容表collection_content...');
$db->query("drop table if exists {$prefix}collection_content");
$sql="create table `{$prefix}collection_content`
(
id smallint unsigned auto_increment primary key,
nodeid smallint unsigned not null default 0,
siteid smallint unsigned not null default 0,
url varchar(50) not null default '',
title varchar(50) not null default '',
common_content text,
attr_content text,
comment_info text,
imgpath text comment '所有下载的图片路径，便于删除',
is_import enum('0','1') not null default '0',
goods_id int unsigned not null default 0 comment '导出的商品详情id'
)";
if($db->query($sql))
	streamout('成功<br/>');
else
{
	$IS_FINISH=FALSE;
	streamout('失败<br/>'); 
}

streamout('插入推荐节点配置...');
/*
$sql="INSERT INTO `{$prefix}collection_node` (`name`, `lastdate`, `siteid`, `catid`, `sourcecharset`, `sourcetype`, `urlpage`, `pagesize_start`, `pagesize_end`, `par_num`, `url_contain`, `url_except`, `url_title_contain`, `url_title_except`, `url_start`, `url_end`, `rule_goods_name`, `rule_shop_price`, `rule_brand`, `rule_goods_weight`, `rule_goods_desc`, `rule_goods_img`, `except_goods_name`, `except_shop_price`, `except_brand`, `except_goods_weight`, `except_goods_desc`, `except_goods_img`, `text_title_sign`, `text_url_sign`, `text_webhead`, `urlpage_single`, `urlpage_multi`, `urlpage_json`, `rule_goods_gallery`, `except_goods_gallery`, `include_goods_name`, `include_shop_price`, `include_brand`, `include_goods_weight`, `include_goods_desc`, `include_goods_gallery`, `include_goods_img`, `desc_img_download`, `goods_img_url_completion`, `is_desc_lazyload`, `rule_desc_lazyload_url`, `include_desc_lazyload_url`, `except_desc_lazyload_url`, `replace_desc_lazyload_url`, `desc_img_url_completion`, `img_src_names`, `desc_url_get_type`, `desc_data_type`, `desc_data_jsonp`, `desc_data_key`, `json_desc_lazyload_url`, `comment_pagenum`, `comment_issuenum`, `comment_url_get_type`, `json_comment_multiload_url`, `comment_data_type`, `comment_start`, `comment_end`, `comment_one_start`, `comment_one_end`, `is_comment_multiload`, `rule_comment_multiload_url`, `include_comment_multiload_url`, `except_comment_multiload_url`, `replace_comment_multiload_url`, `comment_data_jsonp`, `comment_data_key`, `rule_comment_user`, `include_comment_user`, `except_comment_user`, `replace_comment_user`, `rule_comment_content`, `include_comment_content`, `except_comment_content`, `replace_comment_content`, `rule_comment_rank`, `include_comment_rank`, `except_comment_rank`, `replace_comment_rank`, `rule_comment_time`, `include_comment_time`, `except_comment_time`, `replace_comment_time`, `is_timestamp_format`, `rule_comment_extimg`, `include_comment_extimg`, `except_comment_extimg`, `replace_comment_extimg`, `comment_img_download`, `comment_extimg_download`, `comment_img_url_completion`, `replaceto_goods_name`, `replaceto_shop_price`, `replaceto_brand`, `replaceto_goods_weight`, `replaceto_goods_desc`, `replaceto_goods_img`, `replaceto_goods_comment`, `replaceto_goods_gallery`, `replaceto_desc_lazyload_url`, `replaceto_comment_multiload_url`, `replaceto_comment_user`, `replaceto_comment_content`, `replaceto_comment_rank`, `replaceto_comment_time`, `gallery_data_type`, `replaceto_gallery_async_url`, `is_gallery_async`, `gallery_url_get_type`, `json_gallery_async_url`, `rule_gallery_async_url`, `include_gallery_async_url`, `except_gallery_async_url`, `replace_gallery_async_url`, `gallery_start`, `gallery_end`, `is_gallery_download`, `gallery_data_jsonp`, `gallery_data_key`, `gallery_num_limit`, `gallery_src_type`, `gallery_src_type_separator`, `gallery_src_type_key`, `gallery_img_url_completion`, `replace_goods_gallery`, `is_add_comment_user`, `replace_goods_desc`, `replace_goods_name`,`per_collect_num`,`is_create_brand`,`replace_brand`) VALUES
( 'JD-手机', 1502637337, 0, 4, '3', 1, 'https://list.jd.com/list.html?cat=9987,653,655&page=(*)&sort=sort_rank_asc&trans=1&JL=6_0_0&cu=true&abt=3#J_main', 1, 2, 1, 'item.jd.com', 'list.jd.com', '</em>', '', '<ul class=\"gl-warp clearfix\">', '</ul>\r\n  <div class=\"clr\"></div>', '<title>[内容]【[除书写]】-京东</title>', '', '', '', '<div id=\"J-detail-content\">[内容]</div><!-- #J-detail-content -->', '<img id=\"spec-img\" width=\"450\" data-origin=\"[字符]\" alt=\"[除字符]\">', '', '', '', '', '', '', '', '', '', '', '', '', '<img alt=\'[除字符]\' src=\'[字符]\' data-url=\'[除字符]\' data-img=\'1\' width=\'54\' height=\'54\'>', '', '', '', '', '', '', 's54x54_jfs', '', 0, 'http:', 1, 'desc: \'[字符]\',', '//cd.jd.com/description/', '', '', 'http:', 'src,data-lazyload', 1, 1, 0, 'content', '', 2, 11, 0, 'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment&productId=[PID]&score=0&sortType=5&page=(*)&pageSize=10&isShadowSku=0&rid=0&fold=1', 1, '', '', '', '', 1, '', '', '', '', 1, 'comments', '[key]nickname', '', '', '', '[key]content', '', '', '', '[key]score', '', '', '', '[key]creationTime', '', '', '', 1, '[key]images,imgUrl', '', '', '', 0, 0, 'http:', '', '', '', '', 'src', '', '', 's800x800_jfs', '', '', 'src', '', '', '', 0, '', 0, 0, '', '', '', '', '', '<div id=\"spec-list\" class=\"spec-items\">\r\n                        <ul class=\"lh\">', ' </ul>\r\n                    </div>\r\n                </div>\r\n                <div class=\"preview-info\">', 0, 0, '', 0, 0, '', '', 'http:', 's54x54_jfs', 1, 'data-lazyload', '[reg]/【(.*)】/',10,1,'[reg]\/（(.*)）\/')

";
*/

$sql="
INSERT INTO `ecs_collection_node` (`id`, `name`, `lastdate`, `siteid`, `catid`, `sourcecharset`, `sourcetype`, `page_sign_rule`, `urlpage`, `urlpage_single`, `urlpage_multi`, `urlpage_json`, `pagesize_start`, `pagesize_end`, `par_num`, `url_contain`, `url_except`, `url_start`, `url_title_contain`, `url_title_except`, `url_end`, `text_title_sign`, `text_url_sign`, `text_webhead`, `per_collect_num`, `is_create_brand`, `rule_goods_name`, `rule_shop_price`, `rule_brand`, `rule_goods_weight`, `rule_goods_desc`, `rule_goods_img`, `rule_goods_comment`, `include_goods_name`, `include_shop_price`, `include_brand`, `include_goods_weight`, `include_goods_desc`, `include_goods_img`, `include_goods_comment`, `except_goods_name`, `except_shop_price`, `except_brand`, `except_goods_weight`, `except_goods_desc`, `except_goods_img`, `except_goods_comment`, `replace_goods_name`, `replaceto_goods_name`, `replace_shop_price`, `replaceto_shop_price`, `replace_brand`, `replaceto_brand`, `replace_goods_weight`, `replaceto_goods_weight`, `replace_goods_desc`, `replaceto_goods_desc`, `replace_goods_img`, `replaceto_goods_img`, `replace_goods_comment`, `replaceto_goods_comment`, `is_gallery_async`, `gallery_url_get_type`, `json_gallery_async_url`, `rule_gallery_async_url`, `include_gallery_async_url`, `except_gallery_async_url`, `replace_gallery_async_url`, `replaceto_gallery_async_url`, `gallery_data_type`, `gallery_start`, `gallery_end`, `rule_goods_gallery`, `include_goods_gallery`, `except_goods_gallery`, `replace_goods_gallery`, `replaceto_goods_gallery`, `gallery_data_jsonp`, `gallery_data_key`, `gallery_num_limit`, `is_gallery_download`, `gallery_src_type`, `gallery_src_type_separator`, `gallery_src_type_key`, `is_desc_lazyload`, `desc_url_get_type`, `desc_data_type`, `desc_data_jsonp`, `desc_data_key`, `json_desc_lazyload_url`, `rule_desc_lazyload_url`, `include_desc_lazyload_url`, `except_desc_lazyload_url`, `replace_desc_lazyload_url`, `replaceto_desc_lazyload_url`, `comment_pagenum`, `comment_issuenum`, `comment_url_get_type`, `json_comment_multiload_url`, `comment_data_type`, `comment_start`, `comment_end`, `comment_one_start`, `comment_one_end`, `is_comment_multiload`, `rule_comment_multiload_url`, `include_comment_multiload_url`, `except_comment_multiload_url`, `replace_comment_multiload_url`, `replaceto_comment_multiload_url`, `comment_data_jsonp`, `comment_data_key`, `rule_comment_user`, `include_comment_user`, `except_comment_user`, `replace_comment_user`, `replaceto_comment_user`, `is_add_comment_user`, `rule_comment_content`, `include_comment_content`, `except_comment_content`, `replace_comment_content`, `replaceto_comment_content`, `rule_comment_rank`, `include_comment_rank`, `except_comment_rank`, `replace_comment_rank`, `replaceto_comment_rank`, `rule_comment_time`, `include_comment_time`, `except_comment_time`, `replace_comment_time`, `replaceto_comment_time`, `is_timestamp_format`, `rule_comment_extimg`, `include_comment_extimg`, `except_comment_extimg`, `replace_comment_extimg`, `replaceto_comment_extimg`, `desc_img_download`, `comment_img_download`, `comment_extimg_download`, `img_src_names`, `goods_img_url_completion`, `gallery_img_url_completion`, `desc_img_url_completion`, `comment_img_url_completion`) VALUES
(1, 'JD-手机', 1503073606, 0, 4, '3', 1, 'item.jd.com/(*).html', 'https://list.jd.com/list.html?cat=9987,653,655&page=(*)&sort=sort_rank_asc&trans=1&JL=6_0_0&cu=true&abt=3#J_main', '', '', '', 1, 2, 1, 'item.jd.com', 'list.jd.com', '<ul class=\"gl-warp clearfix\">', '</em>', '', '</ul>\r\n  <div class=\"clr\"></div>', '', '', '', 10, 1, '<title>[内容]【[除书写]】-京东</title>', '', '<li title=\'[字符]\'>品牌： <a href=', '', '<div id=\"J-detail-content\">[内容]</div><!-- #J-detail-content -->', '<img id=\"spec-img\" width=\"450\" data-origin=\"[字符]\" alt=\"[除字符]\">', NULL, '', '', '', '', '', '', NULL, '', '', '', '', '', '', NULL, '[reg]/【(.*)】/', '', '', '', '[reg]/（(.*)）/', '', '', '', 'data-lazyload', 'src', '', '', NULL, '', 0, 0, '', '', '', '', '', '', 0, '<div id=\"spec-list\" class=\"spec-items\">\r\n                        <ul class=\"lh\">', '</ul>\r\n                    </div>\r\n                </div>\r\n                <div class=\"preview-info\">', '<img alt=\'[除字符]\' src=\'[字符]\' data-url=\'[除字符]\' data-img=\'1\' width=\'54\' height=\'54\'>', 's54x54_jfs', '', 's54x54_jfs', 's800x800_jfs', 0, '', 0, 0, 0, '', '', 1, 1, 1, 0, 'content', '', 'desc: \'[字符]\',', '//cd.jd.com/description/', '', '', '', 2, 11, 0, 'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment&productId=[PID]&score=0&sortType=5&page=(*)&pageSize=10&isShadowSku=0&rid=0&fold=1', 1, '', '', '', '', 1, '', '', '', '', '', 1, 'comments', '[key]nickname', '', '', '', 'src', 1, '[key]content', '', '', '', '', '[key]score', '', '', '', '', '[key]creationTime', '', '', '', '', 1, '[key]images,imgUrl', '', '', '', '', 0, 0, 0, 'src,data-lazyload', 'http:', 'http:', 'http:', 'http:')
";


if($db->query($sql))
{
	$nodeid=$db->insert_id();
	streamout('成功<br/>');
	
	streamout('插入属性获取配置...');
	$sql="INSERT INTO `{$prefix}collection_diyrule` (`nodeid`, `attrid`, `attrname`, `rule_content`, `rule_include`, `rule_except`, `rule_replace`) VALUES
		({$nodeid}, 78, '摄像头', '<dt>后置摄像头</dt><dd>[标签]</dd>', '', '', ''),
		({$nodeid}, 72, '标准配置', '<h3>包装清单</h3>\r\n                        <p>[标签]</p>', '', '', ''),
		({$nodeid}, 70, '通话时间', '<dt>电池容量（mAh）</dt><dd>[标签]</dd>', '', '', ''),
		({$nodeid}, 69, '操作系统', '<dt>操作系统版本</dt><dd>[标签]</dd>', '', '', ''),
		({$nodeid}, 68, '内存容量', '<dt>ROM</dt><dd>[标签]</dd>', '', '', ''),
		({$nodeid}, 62, '清晰度', '<dt>分辨率</dt><dd>[标签]</dd>', '', '', ''),
		({$nodeid}, 60, '主屏参数/内屏参数', '<dt>主屏幕尺寸（英寸）</dt><dd>[标签]</dd>', '', '', ''),
		({$nodeid}, 57, '支持频率/网络频率', '<dt>网络频率（2G/3G）</dt><dd>[标签]</dd>', '', '', ''),
		({$nodeid}, 56, '网络制式', 'dt>4G网络</dt><dd>[标签]</dd>', '', '', '')";
	if($db->query($sql))
		streamout('成功<br/>');	
	else
	{
		$IS_FINISH=FALSE;
		streamout('失败<br/>'); 
	}
	
	streamout('插入自定义规则配置...');
	$sql="INSERT INTO `{$prefix}collection_field` (`nodeid`, `field_name`, `field_rule_match`, `field_rule_include`, `field_rule_except`, `field_rule_replace`) VALUES
		({$nodeid}, 'PID', 'url=//item.m.jd.com/product/[数字].html', '', '', '')";
	if($db->query($sql))
		streamout('成功<br/>');	
	else
	{
		$IS_FINISH=FALSE;
		streamout('失败<br/>'); 
	}
	
	streamout('插入公共替换规则配置...');
	$sql="INSERT INTO `{$prefix}collection_replace` (`nodeid`, `replace_from`, `replace_to`) VALUES
		({$nodeid}, '京东', '好买商城')";
	if($db->query($sql))
		streamout('成功<br/>');	
	else
	{
		$IS_FINISH=FALSE;
		streamout('失败<br/>'); 
	}
	
}
else
{
	$IS_FINISH=FALSE;
	streamout('失败<br/>'); 
	die('<br/>----------安装失败---------<br/>');
}





if($IS_FINISH)
	streamout('<br>-------------------------安装成功-------------------------</br>');
else
	streamout('<br>-------------------------安装失败-------------------------</br>');


function streamout($str)
{
	echo str_repeat(' ',4096);
	ob_flush();
	flush();
	echo $str;
}
function copyto($from,$to,$forcereplace=true)
{
	streamout('导入'.$from.'...');
	if(file_exists($to)&&!$forcereplace)
	{
		streamout('文件已存在<br/>');
		return FALSE;
	}
	@unlink($to);
	if(file_exists($from))
	{
		if(copy($from,$to))
			streamout('成功<br/>');
		else
		{
			$IS_FINISH=FALSE;
			streamout('失败<br/>'); 
		}
	}
	else
		streamout('文件不存在<br/>');
	
}
function config($tips,$file,$str)
{
	streamout($tips.'...');
	if(!file_exists($file))
	{
		streamout('配置文件不存在<br/>');
		return false;
	}
	$doc=file_get_contents($file);

	if(empty($doc))
	{
		streamout('配置文件异常<br/>');
		return false;
	}
	
	//文件编码转换为系统编码
	$char=mb_detect_encoding($doc,['ASCII','UTF-8','GB2312','GBK','BIG5']);
	$doc = iconv($char, 'UTF-8//TRANSLIT//IGNORE', $doc);

	if(strpos($doc,$str)===FALSE)
	{
		$doc=str_replace('?>',$str.'  

?>',$doc);
		
		//系统编码转换为文件编码
		$doc = iconv('UTF-8', strtoupper($char).'//TRANSLIT//IGNORE', $doc);
		if(file_put_contents($file,$doc)>0)
			streamout('成功<br/>');
		else
		{
			$IS_FINISH=FALSE;
			streamout('失败,配置清空<br/>'); 
		}
	}
	else
		streamout('已经配置，跳过<br/>');
}